$.fn.imagesLoaded = function(callback){
    var elems = this.find('img'),
        len   = elems.length,
        _this = this,
        srcs=[],
        loadImage = function(){
        	
          if ( this.actual && --len <= 0 ) {
            elems.unbind( 'load', loadImage )
            elems.unbind( 'error', onError )
            callback.call( _this );
          }
          this.actual=null;
          
        };
        onError=function(){
        	if ( this.actual && --len <=0 ) {
    		 elems.unbind( 'load', loadImage )
           	 elems.unbind( 'error', onError )
		      callback.call( _this );
		      return _this;
    		}
    		this.actual=null;
        }
       
    // if no images, trigger immediately
    
    if ( !len ) {
    	
      callback.call( this );
      return this;
    }
    
    elems.bind('error', onError).bind( 'load', loadImage ).each(function(){
      // cached images don't fire load sometimes, so we reset src.
      this.actual=true;
      if (this.complete || this.complete === undefined){
      	
        loadImage.call(this);
      }  
    }); 

    return this;
  };